<?php


namespace app\manage\controller;


use app\common\model\PointsLog;
use app\common\service\PointsService;
use think\Db;
use think\Exception;

class Examine extends Base
{
    public function where()
    {
        //获取当前管理员
        $userid     = session('userid', '', session_prefix());
        //获取管理员类型
        $admin_type = \app\common\model\Users::where("id",$userid)->value('type');
        //店长角色
        if ($admin_type == 1){
            $stores_id = \app\common\model\Users::where("id",$userid)->value('stores_id');
            //获取当前门店 店员
            $users = \app\common\model\Users::where("type",2)->where("stores_id",$stores_id)->field('id')->select()->toArray();
            $user_ids = array_column($users,"id");
            $where[] = [
                ["user_id","in",$user_ids]
            ];
        }
        $where[] = [
            ["status","neq",1]
        ];
        return $where;
    }
    public function getModel()
    {
        return (new PointsLog());
    }
    public function order()
    {
        return "status asc,created_at desc";
    }

    //通过
    public function adopt(){
        $id = input('id');
        Db::startTrans();
        try {
            $member_id = PointsLog::where("id",$id)->value('member_id');
            PointsLog::where("id",$id)->update([
                'status' => 1
            ]);
            //重算用户积分
            (new PointsService())->RecalculationMemberPoints($member_id);
            //重算用户总积分
            (new PointsService())->RecalculationMemberTotalPoints($member_id);
            //重置用户等级
            (new PointsService())->ResetMemberRole($member_id);
            Db::commit();
            return j(0,"操作成功");
        } catch (Exception $exception) {
            Db::rollback();
            return j(1,$exception->getMessage());
        }
    }
    //驳回
    public function reject(){
        $id = input('id');
        Db::startTrans();
        try {
            PointsLog::where("id",$id)->update([
                'status' => 2
            ]);
            Db::commit();
            return j(0,"操作成功");
        } catch (Exception $exception) {
            Db::rollback();
            return j(1,$exception->getMessage());
        }
    }
}